

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/2.jpg">
  <link rel="icon" type="image/png" href="/img/2.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="K">
  <meta name="keywords" content="">
  <title>docker-容器持久化 - K</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
    
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>かい</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/1.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-12-07 14:00" pubdate>
        2020年12月7日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.5k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      37
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">docker-容器持久化</h1>
            
            <div class="markdown-body" id="post-body">
              <h2 id="数据持久化"><a href="#数据持久化" class="headerlink" title="数据持久化"></a>数据持久化</h2><ul>
<li><p>在做数据卷的时候有三种写入方式</p>
<blockquote>
<p>1.往容器内写入数据 bind mount （物理机挂载到容器的权限有ro,rw）<br>2.从容器内获取数据 managed volume (容器持久化将数据存储到物理机)<br>3.把容器放到内存中 tmpfs (内存挂载)</p>
</blockquote>
</li>
<li><p>共享 volume container （专门为其他容器提供存储的功能卷） 也叫容器卷</p>
</li>
<li><p>volume nfs （通过nfs服务，共享容器之间的数据，并把容器的数据保存到nfs服务中）</p>
</li>
<li><p>data-packed volume container （镜像持久化，将数据存储到一个docker镜像中）</p>
</li>
</ul>
<h2 id="资源存储"><a href="#资源存储" class="headerlink" title="资源存储"></a>资源存储</h2><ul>
<li>容器的数据分为热数据和冷数据</li>
<li>冷数据 &gt; 就是镜像层的数据</li>
<li>热数据 &gt; 是容器层的数据 （热数据需要存储起来）</li>
</ul>
<h2 id="tmpfs内存挂载"><a href="#tmpfs内存挂载" class="headerlink" title="tmpfs内存挂载"></a>tmpfs内存挂载</h2><ul>
<li>通过 –tmpfs 将容器内的目录挂载到内存中<pre><code class="hljs jboss-cli">docker run -itd <span class="hljs-params">--name</span> tmpfs-test <span class="hljs-params">--tmpfs</span> <span class="hljs-string">/app</span> busybox
<span class="hljs-comment"># 容器内的app目录就挂载到内存了，这时读写速度会非常快</span></code></pre>

</li>
</ul>
<h2 id="bind-mount-主机-gt-容器"><a href="#bind-mount-主机-gt-容器" class="headerlink" title="bind mount (主机-&gt;容器)"></a>bind mount (主机-&gt;容器)</h2><ul>
<li><p>将物理机的内容挂载到容器内</p>
</li>
<li><p>权限有 ro 和 rw</p>
</li>
<li><p>挂载的必须是目录或者文件，不可以是块设备</p>
</li>
<li><p>通过 –volume 或者 -v 进行挂载</p>
<pre><code class="hljs gradle">[root@localhost ~]# mkdir htdocs
[root@localhost ~]# echo <span class="hljs-string">&#x27;lmk&#x27;</span> &gt; htdocs/index.html 
[root@localhost ~]# docker run -itd -p <span class="hljs-number">80</span>:<span class="hljs-number">80</span> --volume <span class="hljs-regexp">/root/</span>htdocs<span class="hljs-regexp">/:/u</span>sr<span class="hljs-regexp">/local/</span>apache2/htdocs httpd
<span class="hljs-number">2</span>d57a0bd883aa4b93764305eb8a5f5ac27c844c95d2096296dd418242f2719cf   
    #  --volume <span class="hljs-regexp">/root/</span>htdocs<span class="hljs-regexp">/:/u</span>sr<span class="hljs-regexp">/local/</span>apache2<span class="hljs-regexp">/htdocs 这里是将 本地的 htdocs 挂载到容器的 /u</span>sr<span class="hljs-regexp">/local/</span>apache2/htdocs内
[root@localhost ~]# curl <span class="hljs-number">192.168</span>.<span class="hljs-number">100.211</span>                                                                               
lmk
[root@localhost ~]# echo <span class="hljs-string">&#x27;kml&#x27;</span> &gt; htdocs/index.html
[root@localhost ~]# curl <span class="hljs-number">192.168</span>.<span class="hljs-number">100.211</span>
kml
</code></pre></li>
<li><p>可以看出已经将本地的目录挂载到容器内了,默认是容器和物理机都可以修改的，可以设置容器不能修改(把容器内的权限设置ro)</p>
</li>
<li><p>设置权限</p>
<pre><code class="hljs elixir">[root<span class="hljs-variable">@localhost</span> ~]<span class="hljs-comment"># docker run -itd --name httpd_1 -p 81:80 --volume /root/htdocs/:/usr/local/apache2/htdocs:ro httpd</span>
<span class="hljs-number">87c465</span>ca5613bbc3934b6330261c7ede1ef2fb598cd1325fce68254c7000652f
<span class="hljs-comment"># 在最后面添加 ro权限，现在容器就修改不了数据了</span>
[root<span class="hljs-variable">@localhost</span> ~]<span class="hljs-comment"># docker exec -it httpd_1 bash</span>
root<span class="hljs-variable">@87c465ca5613</span><span class="hljs-symbol">:/usr/local/apache2</span><span class="hljs-comment"># cd htdocs/</span>
root<span class="hljs-variable">@87c465ca5613</span><span class="hljs-symbol">:/usr/local/apache2/htdocs</span><span class="hljs-comment"># echo &#x27;kkk&#x27; &gt; index.html</span>
<span class="hljs-symbol">bash:</span> <span class="hljs-symbol">index.html:</span> Read-only file system
<span class="hljs-comment"># Read-only 只读的</span></code></pre></li>
<li><p>将文件挂载到容器内</p>
<pre><code class="hljs fortran">[root@localhost ~]# docker run -it --<span class="hljs-keyword">name</span> bbox1 --volume /root/htdocs/<span class="hljs-built_in">index</span><span class="hljs-number">.</span>html:/<span class="hljs-keyword">data</span>/<span class="hljs-built_in">index</span><span class="hljs-number">.</span>html busybox
/ # cd /<span class="hljs-keyword">data</span>/
/<span class="hljs-keyword">data</span> # cat <span class="hljs-built_in">index</span><span class="hljs-number">.</span>html
kml
/<span class="hljs-keyword">data</span> # ls
<span class="hljs-built_in">index</span><span class="hljs-number">.</span>html
# 将物理机的文件挂载到容器内</code></pre></li>
<li><p>查看一下挂载的信息</p>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker inspect httpd_1</span>
        <span class="hljs-string">&quot;Mounts&quot;</span>: [
            &#123;
                <span class="hljs-string">&quot;Type&quot;</span>: <span class="hljs-string">&quot;bind&quot;</span>,  <span class="hljs-meta"># 挂载类型</span>
                <span class="hljs-string">&quot;Source&quot;</span>: <span class="hljs-string">&quot;/root/htdocs&quot;</span>,  <span class="hljs-meta"># 源地址目录(物理机的目录)</span>
                <span class="hljs-string">&quot;Destination&quot;</span>: <span class="hljs-string">&quot;/usr/local/apache2/htdocs&quot;</span>, 被挂载目录(容器的目录)
                <span class="hljs-string">&quot;Mode&quot;</span>: <span class="hljs-string">&quot;ro&quot;</span>, <span class="hljs-meta"># 权限</span>
                <span class="hljs-string">&quot;RW&quot;</span>: <span class="hljs-literal">false</span>, <span class="hljs-meta"># 是否读写（如果设置了ro权限这里就是false）</span>
                <span class="hljs-string">&quot;Propagation&quot;</span>: <span class="hljs-string">&quot;rprivate&quot;</span>  <span class="hljs-meta"># 传播方向 这里是私有挂载</span>
            &#125;
        ]</code></pre>
<h2 id="bind-mount-长格式写法"><a href="#bind-mount-长格式写法" class="headerlink" title="bind mount 长格式写法"></a>bind mount 长格式写法</h2></li>
<li><p>--mount 挂载</p>
</li>
<li><p>type  挂载类型</p>
</li>
<li><p>source    源地址(物理机地址)</p>
</li>
<li><p>destination \target  挂载地址(容器地址) 在这里后面可以加权限 readonly(ro权限)</p>
</li>
<li><p>bind-propagation 传播方向<br> <img src="/images/docker/cbfx.png" srcset="/img/loading.gif"></p>
<blockquote>
<p>开启容器挂载目录</p>
</blockquote>
<pre><code class="hljs gradle">[root@localhost ~]# docker run -itd --name httpd_2 -p:<span class="hljs-number">82</span>:<span class="hljs-number">80</span> --mount type=bind,<span class="hljs-keyword">source</span>=<span class="hljs-regexp">/root/</span>htdocs,target=<span class="hljs-regexp">/usr/</span>share<span class="hljs-regexp">/nginx/</span>html --mount type=bind,<span class="hljs-keyword">source</span>=<span class="hljs-regexp">/root/</span>htdocs,target=<span class="hljs-regexp">/usr/</span>share<span class="hljs-regexp">/nginx/</span>html2,readonly,bind-propagation=rslave nginx  
afd3a75b8e904fa52820d4ec8d73e940bd91a42f3223bea5b39e1725c9edd6e6   
    # 变量写法
    # docker run -itd --name httpd --mount type=bind,<span class="hljs-keyword">source</span>=<span class="hljs-string">&quot;$(pwd)&quot;</span><span class="hljs-regexp">/htdocs,target=/u</span>sr<span class="hljs-regexp">/share/</span>nginx<span class="hljs-regexp">/html --mount type=bind,source=&quot;$ (pwd)&quot;/</span>htdocs,target=<span class="hljs-regexp">/usr/</span>share<span class="hljs-regexp">/nginx/</span>html2,readonly,bind-propagation=rslave nginx

# 将htdocs目录挂载到nginx的html目录 又将htdocs目录挂载到html2目录设置权限为只读，设置传播方向为rslave
[root@localhost ~]# curl <span class="hljs-number">192.168</span>.<span class="hljs-number">100.211</span>:<span class="hljs-number">82</span>
kml</code></pre>
<blockquote>
<p>进入容器</p>
</blockquote>
<pre><code class="hljs elixir">[root<span class="hljs-variable">@localhost</span> ~]<span class="hljs-comment"># docker exec -it httpd_2 bash</span>
root<span class="hljs-variable">@4554a29ba882</span><span class="hljs-symbol">:/</span><span class="hljs-comment"># cd /usr/share/nginx/html2/</span>
root<span class="hljs-variable">@4554a29ba882</span><span class="hljs-symbol">:/usr/share/nginx/html2</span><span class="hljs-comment"># ls</span>
index.html
root<span class="hljs-variable">@4554a29ba882</span><span class="hljs-symbol">:/usr/share/nginx/html2</span><span class="hljs-comment"># echo &#x27;kkk&#x27; &gt; index.html</span>
<span class="hljs-symbol">bash:</span> <span class="hljs-symbol">index.html:</span> Read-only file system
<span class="hljs-comment"># html2 目录不可读写</span></code></pre>
</li>
<li><p>查看挂载信息</p>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker inspect httpd_2</span>
            <span class="hljs-string">&quot;Mounts&quot;</span>: [
                &#123;
                    <span class="hljs-string">&quot;Type&quot;</span>: <span class="hljs-string">&quot;bind&quot;</span>, <span class="hljs-meta"># 类型</span>
                    <span class="hljs-string">&quot;Source&quot;</span>: <span class="hljs-string">&quot;/root/htdocs&quot;</span>, <span class="hljs-meta"># 源目录</span>
                    <span class="hljs-string">&quot;Target&quot;</span>: <span class="hljs-string">&quot;/usr/share/nginx/html&quot;</span> <span class="hljs-meta"># 挂载目录</span>
                &#125;,
                &#123;
                    <span class="hljs-string">&quot;Type&quot;</span>: <span class="hljs-string">&quot;bind&quot;</span>, <span class="hljs-meta"># 类型</span>
                    <span class="hljs-string">&quot;Source&quot;</span>: <span class="hljs-string">&quot;/root/htdocs&quot;</span>, <span class="hljs-meta">#源目录</span>
                    <span class="hljs-string">&quot;Target&quot;</span>: <span class="hljs-string">&quot;/usr/share/nginx/html2&quot;</span>, <span class="hljs-meta">#挂载目录</span>
                    <span class="hljs-string">&quot;ReadOnly&quot;</span>: <span class="hljs-literal">true</span>, <span class="hljs-meta"># 是否只读</span>
                    <span class="hljs-string">&quot;BindOptions&quot;</span>: &#123;
                        <span class="hljs-string">&quot;Propagation&quot;</span>: <span class="hljs-string">&quot;rslave&quot;</span> <span class="hljs-meta"># 传播方向</span>
                    &#125;
                &#125;
            ],</code></pre>

</li>
</ul>
<h2 id="maneged-volume-容器-gt-主机"><a href="#maneged-volume-容器-gt-主机" class="headerlink" title="maneged volume (容器-&gt;主机)"></a>maneged volume (容器-&gt;主机)</h2><ul>
<li>和bind mount 最大的区别就是不需要指定源地址<blockquote>
<p>容器映射出目录</p>
</blockquote>
<pre><code class="hljs gherkin">[root<span class="hljs-meta">@localhost</span> ~]<span class="hljs-comment"># docker run -itd --name httpd -p 80:80 --volume /usr/local/apache2/htdocs httpd </span>
4805dd547134e0a6db1a431d11b7b3153a27feb040bfc134a56e163fd27801db
[root<span class="hljs-meta">@localhost</span> ~]<span class="hljs-comment"># curl 192.168.100.211                                                                          </span>
<span class="hljs-variable">&lt;html&gt;</span><span class="hljs-variable">&lt;body&gt;</span><span class="hljs-variable">&lt;h1&gt;</span>It works!<span class="hljs-variable">&lt;/h1&gt;</span><span class="hljs-variable">&lt;/body&gt;</span><span class="hljs-variable">&lt;/html&gt;</span></code></pre>
<blockquote>
<p>查看容器详细挂载信息</p>
</blockquote>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker inspect httpd</span>
        <span class="hljs-string">&quot;Mounts&quot;</span>: [
            &#123;
                <span class="hljs-string">&quot;Type&quot;</span>: <span class="hljs-string">&quot;volume&quot;</span>, <span class="hljs-meta"># 挂载类型</span>
                <span class="hljs-string">&quot;Name&quot;</span>: <span class="hljs-string">&quot;7b1a6dbcd4660cf545988020050d20f81476f1d6b883dfbabdeb7f0aaaa28524&quot;</span>, <span class="hljs-meta"># 卷的id</span>
                <span class="hljs-string">&quot;Source&quot;</span>: <span class="hljs-string">&quot;/var/lib/docker/volumes/7b1a6dbcd4660cf545988020050d20f81476f1d6b883dfbabdeb7f0aaaa28524/_data&quot;</span>, <span class="hljs-meta"># 映射到物理机的地址</span>
                <span class="hljs-string">&quot;Destination&quot;</span>: <span class="hljs-string">&quot;/usr/local/apache2/htdocs&quot;</span>, <span class="hljs-meta"># 容器内的地址</span>
                <span class="hljs-string">&quot;Driver&quot;</span>: <span class="hljs-string">&quot;local&quot;</span>, <span class="hljs-meta"># </span>
                <span class="hljs-string">&quot;Mode&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,
                <span class="hljs-string">&quot;RW&quot;</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-string">&quot;Propagation&quot;</span>: <span class="hljs-string">&quot;&quot;</span>
            &#125;
        ]</code></pre>
<blockquote>
<p>查看映射目录</p>
</blockquote>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># cd /var/lib/docker/volumes/7b1a6dbcd4660cf545988020050d20f81476f1d6b883dfbabdeb7f0aaaa28524/_data</span>
[root<span class="hljs-symbol">@localhost</span> _data]<span class="hljs-meta"># ls</span>
index.html
[root<span class="hljs-symbol">@localhost</span> _data]<span class="hljs-meta"># echo <span class="hljs-string">&#x27;lmk_hello&#x27;</span> &gt; index.html</span>
[root<span class="hljs-symbol">@localhost</span> _data]<span class="hljs-meta"># curl 192.168.100.211</span>
lmk_hello</code></pre>

</li>
</ul>
<h2 id="volume-container-容器卷"><a href="#volume-container-容器卷" class="headerlink" title="volume container 容器卷"></a>volume container 容器卷</h2><ul>
<li>通过创建一个容器来映射数据，其他容器只需要挂载这个容器映射的数据</li>
<li>通过 –volumes-from 来挂载容器卷</li>
</ul>
<blockquote>
<p>创建容器卷</p>
</blockquote>
<pre><code class="hljs autoit"><span class="hljs-meta"># 查看容器卷</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker volume ls</span>
DRIVER              VOLUME NAME
<span class="hljs-meta"># 创建容器卷</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker create --name vc_data --volume /root/htdocs/:/usr/local/apache2/htdocs --volume /usr/local/apache2/logs busybox</span>
<span class="hljs-number">84</span>bd5d19bfad5dc447012ab4d4aa58523f0529bd32bb3745540eaf3c4b84a348
<span class="hljs-meta"># 将本地的 htdocs目录挂载到容器内，又将容器内的logs目录映射出本机</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker volume ls</span>
DRIVER              VOLUME NAME
<span class="hljs-keyword">local</span>               <span class="hljs-number">954</span>a38573384dfd5741744419d539f0079d5d4a5d1e64431518ba383ccc3b761</code></pre>
<blockquote>
<p>查看容器卷信息</p>
</blockquote>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker inspect vc_data  </span>
        <span class="hljs-string">&quot;Mounts&quot;</span>: [
            &#123;
                <span class="hljs-string">&quot;Type&quot;</span>: <span class="hljs-string">&quot;volume&quot;</span>, <span class="hljs-meta"># 类型</span>
                <span class="hljs-string">&quot;Name&quot;</span>: <span class="hljs-string">&quot;954a38573384dfd5741744419d539f0079d5d4a5d1e64431518ba383ccc3b761&quot;</span>, <span class="hljs-meta"># 卷id</span>
                <span class="hljs-string">&quot;Source&quot;</span>: <span class="hljs-string">&quot;/var/lib/docker/volumes/954a38573384dfd5741744419d539f0079d5d4a5d1e64431518ba383ccc3b761/_data&quot;</span>, <span class="hljs-meta">#映射出的目录</span>
                <span class="hljs-string">&quot;Destination&quot;</span>: <span class="hljs-string">&quot;/usr/local/apache2/logs&quot;</span>, <span class="hljs-meta"># 将容器内的logs目录映射出，到本地的目录</span>
                <span class="hljs-string">&quot;Driver&quot;</span>: <span class="hljs-string">&quot;local&quot;</span>, 
                <span class="hljs-string">&quot;Mode&quot;</span>: <span class="hljs-string">&quot;&quot;</span>, 
                <span class="hljs-string">&quot;RW&quot;</span>: <span class="hljs-literal">true</span>, <span class="hljs-meta">#读写</span>
                <span class="hljs-string">&quot;Propagation&quot;</span>: <span class="hljs-string">&quot;&quot;</span>
            &#125;,
            &#123;
                <span class="hljs-string">&quot;Type&quot;</span>: <span class="hljs-string">&quot;bind&quot;</span>, <span class="hljs-meta"># 类型</span>
                <span class="hljs-string">&quot;Source&quot;</span>: <span class="hljs-string">&quot;/root/htdocs&quot;</span>, <span class="hljs-meta"># 本地目录</span>
                <span class="hljs-string">&quot;Destination&quot;</span>: <span class="hljs-string">&quot;/usr/local/apache2/htdocs&quot;</span>, <span class="hljs-meta">#容器目录</span>
                <span class="hljs-string">&quot;Mode&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,
                <span class="hljs-string">&quot;RW&quot;</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-string">&quot;Propagation&quot;</span>: <span class="hljs-string">&quot;rprivate&quot;</span>
            &#125;
        ]</code></pre>
<blockquote>
<p>创建容器挂载容器卷</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@localhost ~]</span># docker run -itd --name httpd_1 -p <span class="hljs-number">80</span>:<span class="hljs-number">80</span> --volumes-<span class="hljs-keyword">from</span> vc_data httpd
aa99f7a4b8e4367e69a333b67dc39e738ccfc47f655dbf39b7d832c8f12efb64
<span class="hljs-string">[root@localhost ~]</span># docker run -itd --name httpd_2 -p <span class="hljs-number">81</span>:<span class="hljs-number">80</span> --volumes-<span class="hljs-keyword">from</span> vc_data httpd
<span class="hljs-number">52f</span>01ce67bbfb4cf5f254132de25b36b6dfcf2f393e0b30bd07d4ce69698c7dc
<span class="hljs-string">[root@localhost ~]</span># curl <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>
kml
<span class="hljs-string">[root@localhost ~]</span># curl <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>:<span class="hljs-number">81</span>
kml
# 查看映射出的数据
<span class="hljs-string">[root@localhost ~]</span># cd /var/lib/docker/volumes/<span class="hljs-number">954</span>a38573384dfd5741744419d539f0079d5d4a5d1e64431518ba383ccc3b761/_data
<span class="hljs-string">[root@localhost _data]</span># ls
httpd.pid</code></pre>

<h2 id="data-packed-volume-container-镜像持久化"><a href="#data-packed-volume-container-镜像持久化" class="headerlink" title="data-packed volume container 镜像持久化"></a>data-packed volume container 镜像持久化</h2><ul>
<li>将数据存储到镜像中</li>
<li>可以做数据镜像迁移(如果数据较大就不推荐这样做)<blockquote>
<p>编写Dockerfile</p>
</blockquote>
<pre><code class="hljs awk">[root@localhost ~]<span class="hljs-comment"># vim Dockerfile</span>
FROM centos
ADD htdocs <span class="hljs-regexp">/usr/</span>local<span class="hljs-regexp">/apache2/</span>htdocs                                                                                   
VOLUME <span class="hljs-regexp">/usr/</span>local<span class="hljs-regexp">/apache2/</span>htdocs</code></pre>
<blockquote>
<p>制作镜像</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@localhost ~]</span># docker build -t data .
Sending build context to Docker daemon  <span class="hljs-number">7.097</span>MB
Step <span class="hljs-number">1</span>/<span class="hljs-number">3</span> : FROM centos
 ---&gt; <span class="hljs-number">7e6257</span>c9f8d8
Step <span class="hljs-number">2</span>/<span class="hljs-number">3</span> : ADD htdocs /usr/local/apache2/htdocs
 ---&gt; <span class="hljs-number">13e0</span>d3329dc6
Step <span class="hljs-number">3</span>/<span class="hljs-number">3</span> : VOLUME /usr/local/apache2/htdocs
 ---&gt; Running <span class="hljs-keyword">in</span> f31b0a8a8842
Removing <span class="hljs-built_in">int</span>ermediate container f31b0a8a8842
 ---&gt; <span class="hljs-number">31</span>daf73594a4
Successfully built <span class="hljs-number">31</span>daf73594a4
Successfully tagged data:latest</code></pre>
<blockquote>
<p>将镜像创建成一个容器</p>
</blockquote>
<pre><code class="hljs fortran">[root@localhost ~]# docker create --<span class="hljs-keyword">name</span> <span class="hljs-keyword">data</span> <span class="hljs-keyword">data</span>
<span class="hljs-number">6</span>f8bfef0a3d3026bfccfc8f959bdeaaa589d7e71491f6b7ca59ebb522f935b1a</code></pre>
<blockquote>
<p>使用容器卷</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@localhost ~]</span># docker run -d -p <span class="hljs-number">8080</span>:<span class="hljs-number">80</span> --volumes-<span class="hljs-keyword">from</span> data httpd
<span class="hljs-number">1</span>a4f18b2204850689553edf3a932d154e120be32649162b716645dc8a31f5b85
<span class="hljs-string">[root@localhost ~]</span># curl <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>:<span class="hljs-number">8080</span>
kkk</code></pre>

</li>
</ul>
<h2 id="关于selinux标签在volume的使用"><a href="#关于selinux标签在volume的使用" class="headerlink" title="关于selinux标签在volume的使用"></a>关于selinux标签在volume的使用</h2><ul>
<li>不能使用 –mount设置selinux标签，设置selinux标签后ro权限会被忽略</li>
<li>z(小写) 表示挂载内容在多个容器之间共享</li>
<li>Z(大写) 表示挂载内容是私有和非共享的<blockquote>
<p>小写z</p>
</blockquote>
<pre><code class="hljs gradle">[root@localhost ~]# docker run --name web3 -d -p <span class="hljs-number">80</span> --volume <span class="hljs-regexp">/root/</span>htdocs:<span class="hljs-regexp">/usr/</span>local<span class="hljs-regexp">/apache2/</span>htdocs:z httpd
cfeb66d718a3c57fd7b12c218b260654d5ce33508360ddfa6fda6dd846549b03
[root@localhost ~]# docker <span class="hljs-keyword">inspect</span> web3 

        <span class="hljs-string">&quot;Mounts&quot;</span>: [
            &#123;
                <span class="hljs-string">&quot;Type&quot;</span>: <span class="hljs-string">&quot;bind&quot;</span>,
                <span class="hljs-string">&quot;Source&quot;</span>: <span class="hljs-string">&quot;/root/htdocs&quot;</span>,
                <span class="hljs-string">&quot;Destination&quot;</span>: <span class="hljs-string">&quot;/usr/local/apache2/htdocs&quot;</span>,
                <span class="hljs-string">&quot;Mode&quot;</span>: <span class="hljs-string">&quot;z&quot;</span>,
                <span class="hljs-string">&quot;RW&quot;</span>: <span class="hljs-keyword">true</span>,
                <span class="hljs-string">&quot;Propagation&quot;</span>: <span class="hljs-string">&quot;rprivate&quot;</span>
            &#125;
        ]
    # 表示这个<span class="hljs-regexp">/root/</span>htdocs 可以在多个容器内共享</code></pre>
<blockquote>
<p>大写z</p>
</blockquote>
<pre><code class="hljs gradle">[root@localhost ~]# docker run --name web4 -d -p <span class="hljs-number">80</span> --volume <span class="hljs-regexp">/root/</span>htdocs:<span class="hljs-regexp">/usr/</span>local<span class="hljs-regexp">/apache2/</span>htdocs:Z httpd
f07661653f09aea204beb8cd673aaa92480690dba59f84b807b98d2b387468f1
[root@localhost ~]# docker <span class="hljs-keyword">inspect</span> web4 
        <span class="hljs-string">&quot;Mounts&quot;</span>: [
            &#123;
                <span class="hljs-string">&quot;Type&quot;</span>: <span class="hljs-string">&quot;bind&quot;</span>,
                <span class="hljs-string">&quot;Source&quot;</span>: <span class="hljs-string">&quot;/root/htdocs&quot;</span>,
                <span class="hljs-string">&quot;Destination&quot;</span>: <span class="hljs-string">&quot;/usr/local/apache2/htdocs&quot;</span>,
                <span class="hljs-string">&quot;Mode&quot;</span>: <span class="hljs-string">&quot;Z&quot;</span>,
                <span class="hljs-string">&quot;RW&quot;</span>: <span class="hljs-keyword">true</span>,
                <span class="hljs-string">&quot;Propagation&quot;</span>: <span class="hljs-string">&quot;rprivate&quot;</span>
            &#125;
        ]
    # 表示<span class="hljs-regexp">/root/</span>htdocs 挂载到这个容器的数据时不可以在其他容器内共享的</code></pre></li>
<li>这个一般不用<h2 id="–mount-和-–volumes的区别"><a href="#–mount-和-–volumes的区别" class="headerlink" title="–mount 和 –volumes的区别"></a>–mount 和 –volumes的区别</h2></li>
<li>–mount可以有 rw 和ro权限</li>
<li>–volumes 只有一个rw权限</li>
</ul>
<h2 id="docker-volume备份"><a href="#docker-volume备份" class="headerlink" title="docker volume备份"></a>docker volume备份</h2><blockquote>
<p>创建一些目录</p>
</blockquote>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># mkdir /values</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># mkdir /values/test1</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># mkdir /values/test2</span></code></pre>
<blockquote>
<p>创建容器卷将杠杆创建的目录挂载进去</p>
</blockquote>
<pre><code class="hljs autoit"><span class="hljs-meta"># 将本地的values目录挂载到容器内的data目录下</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker create --name back --volume /values/:/data busybox</span>
af2efdde81198c8e807a5156e82006a025db4065f57787d9eb551d17dec12097</code></pre>
<blockquote>
<p>创建容器挂载容器卷</p>
</blockquote>
<pre><code class="hljs coffeescript"><span class="hljs-comment"># 创建容器挂载容器卷 </span>
<span class="hljs-comment"># 并将本地目录的bakcup挂载到容器内的backup中 在容器中运行一个命令 打包 data目录下的数据放到 backup中</span>
[root@localhost ~]<span class="hljs-comment"># docker run --rm --volumes-from back --volume /root/backup/:/backup busybox tar zcvf /backup/backup.t</span>
ar /data
tar: removing leading <span class="hljs-string">&#x27;/&#x27;</span> <span class="hljs-keyword">from</span> member names
data/
data/test1/
data/test2/
<span class="hljs-comment"># 本地查看压缩包</span>
[root@localhost ~]<span class="hljs-comment"># cd backup/</span>
[root@localhost backup]<span class="hljs-comment"># ls</span>
backup.tar
<span class="hljs-comment"># 备份完毕</span></code></pre>


<h2 id="volume-nfs-容器间目录共享"><a href="#volume-nfs-容器间目录共享" class="headerlink" title="volume nfs 容器间目录共享"></a>volume nfs 容器间目录共享</h2><ul>
<li><p>需要一台nfs服务器 和一台docker</p>
<table>
<thead>
<tr>
<th>192.168.100.211</th>
<th>nfs</th>
</tr>
</thead>
<tbody><tr>
<td>192.168.100.212</td>
<td>docker</td>
</tr>
</tbody></table>
<blockquote>
<p>安装nfs</p>
</blockquote>
<pre><code class="hljs elixir">[root<span class="hljs-variable">@localhost</span> ~]<span class="hljs-comment"># mkdir -p /data/nfs</span>
[root<span class="hljs-variable">@localhost</span> ~]<span class="hljs-comment"># yum -y install nfs-utils rpcbind</span>
[root<span class="hljs-variable">@localhost</span> ~]<span class="hljs-comment"># vim /etc/exports</span>
/data/nfs *(rw,no_root_squash,sync)
[root<span class="hljs-variable">@localhost</span> ~]<span class="hljs-comment"># exportfs -r</span>
[root<span class="hljs-variable">@localhost</span> ~]<span class="hljs-comment"># systemctl restart nfs-server</span>
[root<span class="hljs-variable">@localhost</span> ~]<span class="hljs-comment"># showmount -e 192.168.100.211</span>
Export list <span class="hljs-keyword">for</span> <span class="hljs-number">192.168</span>.<span class="hljs-number">100.211</span><span class="hljs-symbol">:</span>
/data/nfs *</code></pre>
<blockquote>
<p>docker 创建nfs容器卷</p>
</blockquote>
<pre><code class="hljs routeros">[root@localhost ~]# docker volume create --driver local --opt <span class="hljs-attribute">type</span>=nfs --opt <span class="hljs-attribute">o</span>=addr=192.168.100.211,rw --opt <span class="hljs-attribute">device</span>=:/data/nfs volume-nfs
volume-nfs</code></pre>
<blockquote>
<p>docker 将 nfs容器卷挂载到容器中</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@localhost ~]</span># docker run -itd --name bbox1 --volume volume-nfs:/nfs busybox
<span class="hljs-number">2213</span>cfd8aa7983cb2b05e6a5805112e31e22842dc6eeb964d96903123abf8111</code></pre>
<blockquote>
<p>修改nfs目录查看容器内是否有文件</p>
</blockquote>
<pre><code class="hljs autoit"><span class="hljs-meta"># docker查看容器数据</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker exec -it bbox1 sh</span>
/ <span class="hljs-meta"># cd /nfs/</span>
/nfs <span class="hljs-meta"># ls</span>
/nfs <span class="hljs-meta">#  </span>

<span class="hljs-meta"># nfs目录创建文件</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># cd /data/nfs/</span>
[root<span class="hljs-symbol">@localhost</span> nfs]<span class="hljs-meta"># echo <span class="hljs-string">&#x27;lmk_hello&#x27;</span> &gt; file</span>

<span class="hljs-meta"># 容器查看</span>
/nfs <span class="hljs-meta"># ls</span>
file
/nfs <span class="hljs-meta"># cat file</span>
lmk_hello
<span class="hljs-meta"># 所有数据在nfs服务器内，容器关闭也不会消失</span></code></pre></li>
</ul>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/docker/">docker</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/docker/">docker</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章是以学习为目的，如果有不对的地方可以一起交流沟通共同学习 邮箱:1248287831@qq.com！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/12/08/docker-compose/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">docker-compose-docker集群的快速编排</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/12/04/docker%E5%8E%9F%E7%94%9F%E7%BD%91%E7%BB%9C/">
                        <span class="hidden-mobile">docker原生网络</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://space.bilibili.com/39165374" target="_blank" rel="nofollow noopener"><span>LMK</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "docker-容器持久化&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>















</body>
</html>
